package com.xbongbong.paas.service.stage.impl;

import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.enums.StageTypeEnum;
import com.xbongbong.paas.pojo.vo.StageGetVO;
import com.xbongbong.paas.service.stage.AbstractStageSettingsStrategy;
import com.xbongbong.paas.service.stage.StageSettingsStrategy;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.constant.StageProcessConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.CustomerStageErrorCodeEnum;
import com.xbongbong.saas.domain.entity.StageWorkEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 成功阶段设置
 * @author 李少杰
 * @version v1.0.0
 * @date 2021/11/22 13:46
 */
@Service("successStageSettingsStrategy")
public class SuccessStageSettingsStrategy extends AbstractStageSettingsStrategy implements StageSettingsStrategy {

    @Override
    public Integer type() {
        return StageTypeEnum.SUCCESS_STAGE.getType();
    }

    @Override
    public StageGetVO get(PaasStageEntity paasStageEntity, List<StageWorkEntity> stageWorkEntityList) throws XbbException {
        return wrapStageEntity(paasStageEntity);
    }

    @Override
    public void check(List<PaasStageEntity> paasStageEntityList, Integer businessType) throws XbbException {
        if (CollectionsUtil.isEmpty(paasStageEntityList)) {
            throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238038, CustomerStageErrorCodeEnum.API_ERROR_238038.getMsg(), StageTypeEnum.SUCCESS_STAGE.getName(), StageTypeEnum.SUCCESS_STAGE.getName());
        }
        if (paasStageEntityList.size() > StageProcessConstant.END_STAGE_MAX_COUNT) {
            throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238062, CustomerStageErrorCodeEnum.API_ERROR_238062.getMsg(), StageProcessConstant.END_STAGE_MAX_COUNT, StageTypeEnum.SUCCESS_STAGE.getName());
        }
        for (PaasStageEntity paasStageEntity : paasStageEntityList) {
            paasStageEntity.setIntoProportion(paasStageEntity.getOutProportion());
        }
    }
}
